import axios from 'axios'
import qs from 'qs'
import {LOGIN_URL,USERS_URL,SESSION} from '../../components/Plug/URL'
const Login = {
    namespaced:true,
    state:{
		loading:false,
		resloading:false
	 },
    getters:{},
    mutations:{},
    actions:{
		doLogin({state},form){
			state.loading=true
			return new Promise((resolve,reject)=>{
				axios({
					// headers: {
					//     "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
					// },
					method:'post',
					url:LOGIN_URL, 
					data:qs.stringify(form)
				}).then(res=>{
					console.log(res.data)
					state.loading=false
					if(res.data.success){
						sessionStorage.setItem(SESSION,JSON.stringify(res.data.data))
						resolve()
					}else{
						reject()
					}
				}).catch(()=>{
					
				})
			})
		},
		doreg({state},form){
			state.resloading=true
			return new Promise((resolve,reject)=>{
				axios({  
					method:"post",
					url: USERS_URL,
					data: form
				}).then(res=>{
					resolve()
					state.resloading=false
				}).catch(()=>{
					reject()
				})
			})
		}
	}
}
export default Login